//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.ruoyi.live.tool.utils;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class CipherUtil {
    public CipherUtil() {
    }

    public String buildCiphertext(String content) {
        String key = this.crypt("itmc");
        if (key != null && key.length() >= 8) {
            key = key.substring(0, 8).toUpperCase();
            return this.printHexString(this.encrypt(content, key));
        } else {
            return null;
        }
    }

    public String desCiphertext(String content, String key) {
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(key.getBytes());
            key = (new BigInteger(1, md.digest())).toString(16);
        } catch (Exception var4) {
            var4.printStackTrace();
        }

        key = key.toUpperCase().substring(0, 8);
        return this.decrypt(content, key);
    }

    public String buildCiphertext1(String content) {
        String key = this.crypt("itmcsoft");
        if (key != null && key.length() >= 8) {
            key = key.substring(0, 8).toUpperCase();
            return this.decrypt(key, content);
        } else {
            return null;
        }
    }

    public byte[] encrypt(String content, String key) {
        try {
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
            cipher.init(1, secretKey, iv);
            return cipher.doFinal(content.getBytes("UTF-8"));
        } catch (Exception var8) {
            var8.printStackTrace();
            return null;
        }
    }

    public String decrypt(String data, String key) {
        try {
            byte[] bytesrc = this.string2hex(data);
            Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
            DESKeySpec desKeySpec = new DESKeySpec(key.getBytes("UTF-8"));
            SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
            SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
            IvParameterSpec iv = new IvParameterSpec(key.getBytes("UTF-8"));
            cipher.init(2, secretKey, iv);
            byte[] retByte = cipher.doFinal(bytesrc);
            return new String(retByte);
        } catch (Exception var10) {
            var10.printStackTrace();
            return null;
        }
    }

    public String printHexString(byte[] b) {
        StringBuffer sBuffer = new StringBuffer();

        for(int i = 0; i < b.length; ++i) {
            String hex = Integer.toHexString(b[i] & 255);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }

            sBuffer.append(hex.toUpperCase());
        }

        return sBuffer.toString();
    }

    public byte[] string2hex(String ss) {
        byte[] digest = new byte[ss.length() / 2];

        for(int i = 0; i < digest.length; ++i) {
            String byteString = ss.substring(2 * i, 2 * i + 2);
            int byteValue = Integer.parseInt(byteString, 16);
            digest[i] = (byte)byteValue;
        }

        return digest;
    }

    public String crypt(String str) {
        if (str != null && str.length() != 0) {
            StringBuffer hexString = new StringBuffer();

            try {
                MessageDigest md = MessageDigest.getInstance("MD5");
                md.update(str.getBytes());
                byte[] hash = md.digest();

                for(int i = 0; i < hash.length; ++i) {
                    if ((255 & hash[i]) < 16) {
                        hexString.append("0" + Integer.toHexString(255 & hash[i]));
                    } else {
                        hexString.append(Integer.toHexString(255 & hash[i]));
                    }
                }
            } catch (NoSuchAlgorithmException var6) {
                var6.printStackTrace();
            }

            return hexString.toString();
        } else {
            return "";
        }
    }
}
